$(document)
		.ready(
				function() {
					var rowNum = 0;
					var contextPath = document.location.pathname;
					var index = contextPath.substr(1).indexOf("/");
					contextPath = contextPath.substr(1, index);
					var basePath = "/" + contextPath;
					var totalCount = $("#totalCount").val();
					var pSize = $("#pageSize").val();
					var pageIndex = $("#pageIndex").val();
					var pageCount = $("#pageCount").val();
					
					/**
					 * 模态窗口关闭时调用
					 */
//					$("#orderDetails").on('hide.bs.modal', function() {
//						$("#baseTab").addClass("active");
//						$("#baseInfor").addClass("active");
//						$("#detailTab").removeClass("active");
//						$("#detail").removeClass("active");
//						$("#returnTab").removeClass("active");
//						$("#returnTab").removeClass("#returnDetail");
//					});
				/**
					 * 保存与修改
					 */
					$("#optBtn").delegate('#saveBtn', 'click', function() {

						$("#cityInfor").attr("method", "post");

						var cityId = $("#cityId").val();

						if (cityId == "") {
							$("#cityInfor").attr("action", "save.do");
						} else {

							$("#cityInfor").attr("action", "update.do");
						}
						
						
							
							$("#cityInfor").submit();
							
				});
					/**
					 * 查看区
					 */
					$("#cityList")
					.delegate(
							'#watchAreaBtn','click',
							function() {
								var citycode = $(this).attr("objId");
								

								var areaUrl = basePath+ "/manage/province/city/area/list.do";
								document.location.href = areaUrl + "?citycode="+ citycode;
								
							});
					
					/**
					 * 删除
					 */
					$("#tools")
					.delegate(
							'#deleteBtn',
							'click',
							function() {
								delChk = $('input[name="cityId"]:checked');

								if (delChk.length > 0) {
									if (confirm("Are you sure delete the record?")) {

										var ids = "";
										var delUrl = "delete.do";
                                        var idArr=new Array();
                                       
                                        
                                        for (var i = 0; i < delChk.length; i++) {
                                        	idArr.push(delChk.get(i).value);
                                   
									}
										
										$.ajax({
													url : delUrl,
													type: "POST",
													data : {
														ids : idArr.toString()
													},
													cache : false,

													success : function(html) {
														alert("Delete successfully");
														refresh();
													},
													error : function(html) {
														alert("Delete failly");
														refresh();
													}
												});
									} else {
										return;
									}
								} else {
									alert("Please select the record to delete");
									return;
								}
							});
	
					$('#pagination4')
							.jqPaginator(
									{
										totalCounts:totalCount,
										pageSize:pSize,
										visiblePages : pageCount,
										currentPage : 1,
										prev : '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
										page : '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
										next : '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
										onPageChange : function(num, type) {
											
											$("#pageIndex").val(num);
											var provincecode = $("#provinceCode").val();
											var queryUrl = "queryByPage.do";
											$.ajax({
											url : queryUrl,
											dataType : 'json',
											data : {
												pageIndex : num,
												pageSize : pSize,
												provincecode:provincecode
											},
											cache : false,
											success : function(data) {
												$(
														"#cityList tr:not(:first)").remove();
												
												var results = data.pageResult.resultList;
												
												for (i = 0; i < results.length; i++) {
													var city = results[i];
													var rowContent = "<tr>"
															+ "<td><input name='cityId' class='checkbox' type='checkbox' value='"
															+ city.id
															+ "'/></td>"
															+ "<td>"
															+ city.code
															+ "</td>"
															+ "<td>"
															+ city.name
															+ "</td>"
															+ "<td>"
															+ city.provincecode
															+ "</td>"
															rowContent = rowContent	+ 
															"<td><button name='editBtn' id='editBtn' objId='"+city.id+"' class='btn btn-info btn-danger' >修改</button></td>"+
															"<td><button name='lookBtn' id='editBtn' objId='"+city.id+"' class='btn btn-warning btn-large'>查看</button></td>"+
															"<td><button name='watchAreaBtn' id='watchAreaBtn' objId='"+city.code+"' class='btn btn-sm-block btn-sm-info'>查看区</button></td>"
															+ "</tr>";
															
													$("#cityList").append(rowContent);
												}

											},
											error : function(html) {
												return;
											}
										});
										}
									});

					/**

					*//**
					 * 全选当前页记录
					 */
					$("#cityList").delegate('#selectAll', 'click', function() {
						var isSelectAll = $(this).prop('checked');
						$("input[name='cityId']").each(function() {
							$(this).prop("checked", isSelectAll);
						});
					});
					
					function clearItemTable() {

						$("#itemsTable tr:not(:first)").remove();
					}

					/**
					 * 修改按钮事件
					 */

					$("#cityList")
							.delegate(
									'#editBtn',
									'click',
									function() {
										var cityId = $(this).attr("objId");
										
										//获取订单详情
										var editUrl = "show.do?";
										$.ajax({

											url : editUrl,
											type : "POST",
											dataType : 'json',
											data : {
												id : cityId
											},
											cache : false,

											success : function(data) {
												loadCity(data);
											},
											error : function(html) {
												alert("Load Data failly");
												refresh();
											}
										});

									});
					
					
				
				

					$("#tools").delegate('#addCity', 'click', function() {
						var provincecode = $("#provinceCode").val();
						$("#cityInfor input").each(function() {
							$(this).prop("value", null);
							$(this).prop("readonly", false);
							

						});
						$("#provincecode").val(provincecode);
						
						$("#cityInfors").modal('show');
					});


				

					
function loadCity(city) {
	$("#cityId").val(city.id);
	$("#code").val(city.code);
	$("#name").val(city.name);
	$("#provincecode").val(city.provincecode);
	$("#cityInfors").modal('show');
}

function vaildate(form) {
	return true;
}

function refresh() {
	window.location.reload();
}

				});
